package com.sinosoft.jwjw.component.service.imp.system;

import com.sinosoft.jwjw.component.service.system.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * redis sdk默认方法样例
 * redis规则：
 * 登录用户信息：登录后添加，有效2小时，之后过期，操作刷新结束时间。规则：USER_{userid}
 * 用户登录后菜单：登录后添加，有效2小时，之后过期，操作刷新结束时间。规则：USER_MENU_{userid}_{systemid}
 */
@Service
public class RedisServicesImp implements RedisService {


    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Resource(name = "stringRedisTemplate")
    ValueOperations<String, String> valOpsStr;

    @Autowired
    RedisTemplate<Object, Object> redisTemplate;

    @Resource(name = "redisTemplate")
    ValueOperations<Object, Object> valOpsObj;

    /**
     * redis 默认超时时间
     */
    @Value("${default.redisTimeout}")
    private Long DEFAULT_TIMEOUT;


    //用户登录后-用户信息 redis get set
    public String getSUser(String userId) {
        return getStr("USER_" + userId);
    }

    public void setSUser(String userId, String val) {
        setStr("USER_" + userId, val , DEFAULT_TIMEOUT);
    }

    //用户登录后-用户某个系统的菜单
    public String getSUserSystemMenu(String userId, String systemId) {
        return getStr("USER_MENU_" + userId + "_" + systemId);
    }

    public void setSUserSystemMenu(String userId, String systemId, String val) {
        setStr("USER_MENU_" + userId + "_" + systemId, val, DEFAULT_TIMEOUT);
    }

    public void refreshUser(String userId, String systemId) {
        setExpire("USER_" + userId);
        setExpire("USER_MENU_" + userId + "_" + systemId);
    }
    /**
     * 用户登出
     * @param userId
     * @param systemId
     */
    public void getSUserLogout(String userId, String systemId) {
        del("USER_" + userId);
        del("USER_MENU_" + userId + "_" + systemId);
    }


    /**
     * 根据指定key获取String
     *
     * @param key
     * @return
     */
    public String getStr(String key) {
        return valOpsStr.get(key);
    }

    /**
     * 设置Str缓存
     *
     * @param key
     * @param val
     */
    public void setStr(String key, String val) {
        valOpsStr.set(key, val);
    }

    public void setStrDefault(String key, String val) {
        Long timeout = DEFAULT_TIMEOUT;
        setStr(key, val, timeout, TimeUnit.SECONDS);
    }

    public void setStr(String key, String val, Long seconds) {
        setStr(key, val, seconds, TimeUnit.SECONDS);
    }

    public void setStr(String key, String val, Long l, TimeUnit tu) {
        valOpsStr.set(key, val, l, tu);
    }

    public void setExpire(String key) {
//        Long l = Long.parseLong(DEFAULT_TIMEOUT);
        setExpire(key, DEFAULT_TIMEOUT, TimeUnit.SECONDS);
    }

    public void setExpire(String key, Long seconds) {
        setExpire(key, seconds, TimeUnit.SECONDS);
    }

    public void setExpire(String key, Long seconds, TimeUnit tu) {
        redisTemplate.expire(key, seconds, tu);
    }

    /**
     * 删除指定key
     *
     * @param key
     */
    public void del(String key) {
        stringRedisTemplate.delete(key);
    }

    /**
     * 根据指定o获取Object
     *
     * @param o
     * @return
     */
    public Object getObj(Object o) {
        return valOpsObj.get(o);
    }

    /**
     * 设置obj缓存
     *
     * @param o1
     * @param o2
     */
    public void setObj(Object o1, Object o2) {
        valOpsObj.set(o1, o2);
    }

    /**
     * 删除Obj缓存
     *
     * @param o
     */
    public void delObj(Object o) {
        redisTemplate.delete(o);
    }

}
